//
//  ScanResultViewController.swift
//  TestDemo
//
//  Created by 赵洋 on 2017/6/7.
//  Copyright © 2017年 赵洋. All rights reserved.
//

import UIKit
import Alamofire

import MBProgressHUD



class ScanResultViewController: UIViewController,UINavigationBarDelegate{
    var scanResult:String!

    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var typeLabel: UILabel!
    @IBOutlet weak var numberLabel: UILabel!
    
    @IBOutlet weak var amountLabel: UILabel!
    
    @IBOutlet weak var stepper: UIStepper!

    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        let backItem = UIBarButtonItem(title: "返回", style: .plain, target: self, action: #selector(back))
        navigationItem.leftBarButtonItem = backItem
        //navigationItem.backBarButtonIt
        // Do any additional setup after loading the view.
        
        
        guard  let username = User.shareInstance.name else {
            return
        }
        let parameter = ["device":"ios","jienumber":"1","username":username,"id":scanResult,"search":"1"]  as [String:String]
        MBProgressHUD.showLoadingWith(Text: "加载中...")
        Alamofire.request("http://139.199.100.228/jieyuejilu_add.php", method: .post, parameters: parameter).responseJSON { [unowned self] response in
            if let JSON = response.result.value , let result = JSON as? [String : Any]{
                MBProgressHUD.hideLoading()
                let kucunxinxi = result["kucunxinxi"] as! String
                let qicaifenlei = result["qicaifenlei"] as! String
                let qicainame = result["qicainame"] as! String
                self.nameLabel.text = qicainame
                self.typeLabel.text = qicaifenlei
                self.amountLabel.text = kucunxinxi
                self.stepper.maximumValue = Double(kucunxinxi)!
            }
        }
    }

    @IBAction func stepperValueChanged(_ sender: UIStepper) {
            numberLabel.text = String(Int(sender.value))
            debugPrint(sender.stepValue)
        
    }


    @IBAction func sureAction(_ sender: Any) {
        
        guard  let username = User.shareInstance.name else {
            return
        }
        let parameter = ["device":"ios","jienumber":stepper.value,"username":username,"id":scanResult]  as [String:Any]
        
        MBProgressHUD.showLoadingWith(Text: "请求中...")
        Alamofire.request("http://139.199.100.228/jieyuejilu_add.php", method: .post, parameters: parameter).responseJSON { [unowned self] response in
            MBProgressHUD.hideLoading()
            if let JSON = response.result.value , let result = JSON as? [String : Int]{
                guard let flag = result["flag"] , flag == 1 else{
                    if let flag = result["flag"], flag == 2 {
                        MBProgressHUD.showErrorWith(text: "库存不足")
                    }else{
                        MBProgressHUD.showErrorWith(text: "网络异常")
                    }
                    return
                }
                MBProgressHUD.showSuccessWith(text: "租借成功")
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3, execute: { 
                     self.navigationController?.popToRootViewController(animated: true)
                })
                
               
            }else{
                MBProgressHUD.showErrorWith(text: "网络异常")
            }
        }
    }
    
    func back()  {
        navigationController?.popToRootViewController(animated: true)
    }
}
